import axios from "axios";
import store from "@/store";
import router from "@/router";

const axiosInstance = axios.create({
  baseURL: baseURL,
  timeout: 5000,
});
//添加请求拦截器
axiosInstance.interceptors.request.use((config) => {
  config.headers["softeem_ele_client_token"] = store.getters.token;
  return config;
});

axiosInstance.interceptors.response.use(
  (resp) => {
    if (resp.data.status === "success") {
      return Promise.resolve(resp.data.data);
    } else {
      console.log("服务器请求成功，但是status没有成功");
      console.log(resp.data);
      return Promise.reject(resp.data);
    }
  },
  (error) => {
    //这一种情况就是服务器错误，后端错误，如500,403,404等
    console.log(error);
    if (error.response.status == 403) {
      //说明没有权限了，用户要重新登录
      router.replace({
        name: "Login",
      });
    } else {
      return Promise.reject(error);
    }
  }
);

export default axiosInstance;
